/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.cms.entity;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.EmbeddedId;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Transient;

import com.jinhe.tss.cms.CMSConstants;
import com.jinhe.tss.core.Config;
import com.jinhe.tss.core.persistence.IEntity;
import com.jinhe.tss.core.sso.context.Context;
import com.jinhe.tss.core.web.dispaly.grid.GridAttributesMap;
import com.jinhe.tss.core.web.dispaly.grid.IGridNode;

/** 
 * <p> 文件附件Attachment实体对象</p>
 */
@Entity
@Table(name = "cms_attachment")
public class Attachment implements IEntity, IGridNode {
    
    @EmbeddedId
	private AttachmentId id;			//复合主键
    
    @Column(nullable = false)
    private Integer type;				//附件类型	1：图片 2：office文档
    
    @Column(nullable = false)
    private String name;				//附件名称
    
    @Column(nullable = false)
    private String fileName;			//选择上传的文件名		text
    private String fileExt;				//文件后缀	.gif
   
    @Column(nullable = false)
    private String appCode;				//应用Code
    
    @Column(nullable = false)
    private String url;                 //值默认为 "/download.fun?id="
    
    @Column(nullable = false)
    private String localPath;
    
    private Date   uploadDate;			//上传日期
    
    @Transient
    private String uploadName; //上传后生成的下载路径
    
    public String toString(){
        return "(id:(" + this.id + "), name:" + this.name 
            + ", type:" + this.type + ", localPath:" + this.localPath + ", fileName:" + this.fileName + ")" ; 
    }
 
	public String getAppCode() {
		return appCode;
	}
 
	public void setAppCode(String appCode) {
		this.appCode = appCode;
	}
 
	public String getLocalPath() {
		return localPath;
	}
 
	public void setLocalPath(String localPath) {
		this.localPath = localPath;
	}
 
	public String getUrl() {
		return url;
	}
 
	public void setUrl(String url) {
		this.url = url;
	}
 
    public AttachmentId getId() {
        return id;
    }
 
    public void setId(AttachmentId attachmentId) {
        this.id = attachmentId;
    }
 
    public String getFileExt() {
        return fileExt;
    }
 
    public void setFileExt(String fileExt) {
        this.fileExt = fileExt;
    }
 
    public String getFileName() {
        return fileName;
    }
 
    public void setFileName(String fileName) {
        this.fileName = fileName;
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String name) {
        this.name = name;
    }
 
    public Integer getType() {
        return type;
    }
 
    public void setType(Integer type) {
        this.type = type;
    }
 
    public Date getUploadDate() {
        return uploadDate;
    }
 
    public void setUploadDate(Date uploadDate) {
        this.uploadDate = uploadDate;
    }
    
    public boolean isImage(){
        return CMSConstants.ATTACHMENTTYPE_PICTURE.equals(type);
    }

    public boolean isOfficeDoc(){
        return CMSConstants.ATTACHMENTTYPE_OFFICE.equals(type);
    }
    
    public static boolean isImage(Integer type){
        return CMSConstants.ATTACHMENTTYPE_PICTURE.equals(type);
    }

    public static boolean isOfficeDoc(Integer type){
        return CMSConstants.ATTACHMENTTYPE_OFFICE.equals(type);
    }
    
    public GridAttributesMap getAttributes(GridAttributesMap map) {
        map.put("seqNo", id.getSeqNo());
        if(fileName != null){
            map.put("fileName", fileName.substring(fileName.indexOf("_") + 1));
        }
        map.put("fileExt", fileExt == null ? "" : fileExt.toLowerCase());
        map.put("uploadName", uploadName);
        map.put("type", type);
        map.put("name", name);
        return map;
    }
 
	public String getUploadName() {
		return uploadName;
	}
	public void setUploadName(String uploadName) {
		this.uploadName = uploadName;
	}
    
    /**
     * 返回格式类似：http://localhost:8088/cms/download.fun?id=1216&seqNo=1 
     * @param baseUrl
     * @return 
     */
    public String getDownloadUrl(String baseUrl){
        if( this.appCode.equalsIgnoreCase(Config.getAttribute(Config.APPLICATION_CODE)) ) {
            return baseUrl + this.getUrl() + this.id.getArticleId() + "&seqNo=" + this.id.getSeqNo();
        }
        
        return "";
    }
    
    /**
     * 返回格式类似：http://localhost:8088/cms/download.fun?id=1216&seqNo=1 
     * @param baseUrl
     * @return 
     */
    public String getDownloadUrl(){
        String baseUrl =  Context.getApplicationContext().getCurrentAppServer().getBaseURL();
        return this.getDownloadUrl(baseUrl);
    }
    
    /**
     * 返回格式类似：download.fun?id=1216&seqNo=1 
     * @param baseUrl
     * @return 
     */
    public String getRelationDownloadUrl(){
        String temp = this.getUrl().substring(1); //去掉 '/download.fun?id=' 的 '/'
        return temp + this.id.getArticleId() + "&seqNo=" + this.id.getSeqNo();
    }
}